<?php

namespace app\api\controller\v35;

use app\api\controller\v2\Auth;
use app\common\cachemodel\zl2\Zl2TimeNote;
use app\common\lib\Util;

/**
 * 准时提醒
 */
class TimeNote extends Auth
{
    /**
     * 获取准时提醒 记录
     */
    public function getList()
    {
        $timeNoteModel = new Zl2TimeNote();
        $pageData = $timeNoteModel->getListPage($this->user['id'], 'time', ['id', 'time', 'note']);

        $data = [];
        foreach ($pageData['data'] as $item)
        {
            $data[] = [
                'id' => (int)$item['id'],
                'time' => (int)$item['time'],
                'content' => $item['note'],
            ];
        }

        $pageData['data'] = $data;

        return show(config('code.success'), '请求成功', $pageData);
    }

    /**
     * 添加准时提醒
     */
    public function create()
    {
        $date = input('date/s', '');
        $content = input('content/s', '');

        if (empty($date))
        {
            return show(config('code.error'), '请选择提醒时间');
        }

        if (empty($content))
        {
            return show(config('code.error'), '请输入提醒内容');
        }

        if (mb_strlen($content) > 32)
        {
            return show(config('code.error'), '添加提醒失败，提醒内容最多32字');
        }

        if (Util::commentCheck($this->user['id'], ['time' => strtotime($date), 'note' => $content], 5))
        {
            return show(config('code.error'), '操作频繁，请稍后再试～');
        }

        $d = [
            'uid' => $this->user['id'],
            'time' => strtotime($date),
            'note' => $content,
            'create_time' => $this->request->time()
        ];

        $id = (new Zl2TimeNote())->saveData($this->user['id'], $d);

        if ($id)
        {
            return show(config('code.success'), '请求成功', [
                'id' => $id,
                'time' => $d['time'],
                'content' => $content
            ]);
        } else
        {
            return show(config('code.error'), '服务异常，请稍后再试～');
        }
    }

    /**
     * 更新准时提醒
     */
    public function update()
    {
        $id = input('id/d', 0);
        $date = input('date/s', '');
        $content = input('content/s', '');
        if ($id < 1)
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }
        if (empty($date))
        {
            return show(config('code.error'), '请选择提醒时间');
        }

        if (empty($content))
        {
            return show(config('code.error'), '请输入提醒内容');
        }
        if (mb_strlen($content) > 32)
        {
            return show(config('code.error'), '添加提醒失败，提醒内容最多32字');
        }

        $timeNoteModel = new Zl2TimeNote();

        $info = $timeNoteModel->getInfoById($id);

        if (!$info || $info['uid'] !== $this->user['id'])
        {
            return show(config('code.error'), '数据不存在，更新失败～');
        }

        $d = [
            'time' => strtotime($date),
            'note' => $content,
            'update_time' => $this->request->time()
        ];

        $res = (new Zl2TimeNote())->updateData($id, $d);

        if ($res)
        {
            return show(config('code.success'), '更新成功', [
                'id' => $id,
                'time' => $d['time'],
                'content' => $content
            ]);
        } else
        {
            return show(config('code.error'), '服务异常，请稍后再试～');
        }
    }

    /**
     * 删除准时提醒
     */
    public function delete()
    {
        $id = input('id/d', 0);
        if ($id < 1)
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }

        $timeNoteModel = new Zl2TimeNote();

        $info = $timeNoteModel->getInfoById($id);

        if (!$info || $info['uid'] !== $this->user['id'])
        {
            return show(config('code.error'), '数据不存在，或已删除');
        }

        $res = $timeNoteModel->delData($this->user['id'], $id);

        if ($res)
        {
            return show(config('code.success'), '删除成功');
        } else
        {
            return show(config('code.error'), '服务异常，请稍后再试～');
        }
    }
}